package com.kehongborui.ticket.controller;

import com.kehongborui.ticket.entity.Ticket;
import com.kehongborui.ticket.model.ResultModel;
import com.kehongborui.ticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-08-15
 */

@RestController
@RequestMapping("/ticket")
@CrossOrigin(origins = "*", maxAge = 3600) // 处理跨域
public class TicketController {
    @Autowired
    private TicketService ticketService;

    //查找车票
    @GetMapping("/{id}")      // 获取uri中的数据作为参数
    public ResultModel findById(@PathVariable("id") Long id) {
        return new ResultModel(ticketService.findById(id));
    }

    // 查询所有车票
    @GetMapping
    public ResultModel findAll() {
        return new ResultModel(ticketService.findAll());
    }

    // 创建车票
    @PostMapping
    public ResultModel create(@RequestBody Ticket ticket) {
        boolean result = ticketService.create(ticket);
        if (result) {  // 创建车票成功
            return new ResultModel();
        }

        return new ResultModel("failed");
    }

    //
    // 删除车票
    @DeleteMapping("/{id}")
    public ResultModel delete(@PathVariable("id") Long id) {
        boolean result = ticketService.delete(id);
        if (result) {  // 删除车票成功
            return new ResultModel();
        }

        return new ResultModel("failed");
    }

    // 修改
    @PutMapping("/{id}")
    public ResultModel update(@PathVariable("id")Long id, @RequestBody Ticket ticket) {
        boolean result = ticketService.update(ticket);
        if (result) {  // 创建车票成功
            return new ResultModel();
        }

        return new ResultModel("failed");
    }
}
